package com.springboot.cloud.sysadmin.organization.service.impl;

//import com.alicp.jetcache.anno.CacheInvalidate;
//import com.alicp.jetcache.anno.CacheType;
//import com.alicp.jetcache.anno.CacheUpdate;
//import com.alicp.jetcache.anno.Cached;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.sysadmin.organization.dao.PositionMapper;
import com.springboot.cloud.sysadmin.organization.entity.param.PositionQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.Position;
//import com.springboot.cloud.sysadmin.organization.exception.PositionNotFoundException;
import com.springboot.cloud.sysadmin.organization.service.IPositionService;
import com.springboot.cloud.sysadmin.organization.service.IUserPositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
@Service
@Slf4j
public class PositionService extends ServiceImpl<PositionMapper, Position> implements IPositionService {

    @Autowired
    private IUserPositionService userPositionService;

    @Override
    public boolean add(Position position) {
        Position position1 = this.getOne(new QueryWrapper<Position>().eq(StrUtil.isNotBlank(position.getName()), "name", position.getName()), false);
        if (ObjectUtil.isNull(position1)) {
            boolean isSuccess = this.saveOrUpdate(position);
            return isSuccess;
        }
        return false;
    }

    @Override
//    @CacheInvalidate(name = "position::", key = "#id")
    public boolean delete(Set<String> ids) {

        return this.removeByIds(ids);
    }

    @Override
//    @CacheInvalidate(name = "position::", key = "#position.id")
    public boolean update(Position position) {
        Position position1 = this.getOne(new QueryWrapper<Position>().eq(StrUtil.isNotBlank(position.getName()), "name", position.getName()), false);
        if (ObjectUtil.isNull(position1) || position1.getId().equals(position.getId()) && get(position.getId())) {
            return this.updateById(position);
        }
        return false;
    }

    @Override
//    @Cached(name = "position::", key = "#id", cacheType = CacheType.BOTH)
    public Position getPosition(String id) {
        return this.getById(id);
    }


    @Override
    public boolean get(String id) {
        Position position = this.getById(id);
        if (ObjectUtil.isNull(position)) {
            return false;
        }
        return true;
    }

    @Override
    public List<Map<String, Object>> getAll() {
        LambdaQueryWrapper<Position> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Position::getId, Position::getName);
        return this.listMaps(queryWrapper);
    }

    @Override
//    @Cached(name = "position4user::", key = "#userId", cacheType = CacheType.BOTH)
    public List<Position> query(String userId) {
        Set<String> positionIds = userPositionService.queryByUserId(userId);
        return (List<Position>) this.listByIds(positionIds);
    }

    @Override
    public List<Position> query(PositionQueryParam positionQueryParam) {
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(positionQueryParam.getName()), "name", positionQueryParam.getName());
        return this.list(queryWrapper);
    }

    @Override
    public IPage<Position> query(Page page, PositionQueryParam positionQueryParam) {
        SimpleDateFormat convert = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Position> queryWrapper = positionQueryParam.build();
        queryWrapper.like(StrUtil.isNotBlank(positionQueryParam.getCode()), "code", positionQueryParam.getCode());
        queryWrapper.like(StrUtil.isNotBlank(positionQueryParam.getName()), "name", positionQueryParam.getName());
        queryWrapper.like(StrUtil.isNotBlank(positionQueryParam.getDescription()), "description", positionQueryParam.getDescription());
        queryWrapper.eq(StrUtil.isNotBlank(positionQueryParam.getStatus()), "status", positionQueryParam.getStatus());
        queryWrapper.eq(StrUtil.isNotBlank(positionQueryParam.getDeleted()), "deleted", positionQueryParam.getDeleted());
        if (positionQueryParam.getCreatedTimeStart() != null && positionQueryParam.getCreatedTimeEnd() != null) {
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') >= date_format('" + convert.format(positionQueryParam.getCreatedTimeStart()) + "','%Y-%m-%d')");
            queryWrapper.apply("date_format(created_time,'%Y-%m-%d') <= date_format('" + convert.format(positionQueryParam.getCreatedTimeEnd()) + "','%Y-%m-%d')");
        }
        return this.page(page, queryWrapper);
    }
}
